//
//  ZXComposeToolBar.m
//  ZXOCWeiBo
//
//  Created by mac on 15/12/7.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXComposeToolBar.h"

@interface ZXComposeToolBar()

@property(nonatomic,weak)UIButton* btn;

@end

@implementation ZXComposeToolBar

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        //设置背景 colorWithPatternImage平铺图片
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_toolbar_background"]];
        
        //初始化五个按钮
        [self setupBtns];
    }
    return self;
}

-(void)setupBtns
{
    //图片选择
    [self addBtnWithNmlImg:@"compose_toolbar_picture" hltImg:@"compose_toolbar_picture_highlighted"];
    //@
     [self addBtnWithNmlImg:@"compose_mentionbutton_background" hltImg:@"compose_mentionbutton_background_highlighted"];
    //井号
     [self addBtnWithNmlImg:@"compose_trendbutton_background" hltImg:@"compose_trendbutton_background_highlighted"];
    //笑脸
     [self addBtnWithNmlImg:@"compose_emoticonbutton_background" hltImg:@"compose_emoticonbutton_background_highlighted"];
//    加号
     [self addBtnWithNmlImg:@"compose_addbutton_background" hltImg:@"compose_addbutton_background_highlighted"];

}

-(void)addBtnWithNmlImg:(NSString *)nmlImg hltImg:(NSString *)hltImg
{
    UIButton * btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:nmlImg] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:hltImg] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = self.subviews.count;
    [self addSubview:btn];
    self.btn = btn;
}

-(void)btnClick:(UIButton *)btn
{
    NSInteger btnTag = btn.tag;
    
    if ([self.delegate respondsToSelector:@selector(composeToolBar:btnType:)])
    {
        [self.delegate composeToolBar:self btnType:btnTag];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat btnW = self.w / self.subviews.count;
    CGFloat btnH = self.h;
    int i = 0;
    
    for (UIButton * btn in self.subviews)
    {
        CGFloat btnX = btnW * i;
        CGFloat btnY = 0;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        i++;
    }
}


@end
